<%@ page import="com.pb.news.entity.News" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.pb.news.service.impl.NewsServiceImpl" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="java.io.File" %><%--
  Created by IntelliJ IDEA.
  User: Jovi
  Date: 2018/3/23
  Time: 下午3:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%@include file="../common/common.jsp" %>
<head>
    <title>Title</title>
</head>
<body>
<%--/* 接收增加的新闻信息，并调用后台方法，将新闻信息插入数据库 */--%>
<%--request.setCharacterEncoding("utf-8");--%>
<%--int categoryId = Integer.parseInt(request.getParameter("categoryId"));--%>
<%--String title = request.getParameter("title");--%>
<%--String author = request.getParameter("author");--%>
<%--String summary = request.getParameter("summary");--%>
<%--String newscontent = request.getParameter("newscontent");--%>

<%--/* 将新闻信息封装为一个新闻对象 */--%>
<%--News news = new News();--%>
<%--news.setAuthor(author);--%>
<%--news.setTitle(title);--%>
<%--news.setCategoryId(categoryId);--%>
<%--news.setSummary(summary);--%>
<%--news.setContent(newscontent);--%>

<%--news.setCreateDate(new Date());--%>

<%--//调后台方法--%>
<%--boolean flag = newsService.addNews(news);--%>
<%--if (flag) {--%>
<%--%>--%>
<%--<jsp:forward page="newsDetailList.jsp"/>--%>
<%--<%--%>
<%--}--%>
<%
    boolean bRet = false;
    boolean bUpload = false;
    String uploadFileName = "";
    String fieldName = "";
    News news = new News();
    news.setCreateDate(new Date());
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);//当表单是以多部分方式提交的时候为true

    /*
    * request.getSession().getServletContext().getRealPath("/")
    * 是指tomcat下webapps下的项目根路径
    * */
    String uploadFilePath = request.getSession().getServletContext().getRealPath("/upload/");//获取上传图片文件夹路径
    if (isMultipart == true) {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> fileItems = upload.parseRequest(request);//将请求对象转换成集合
        for (FileItem fileItem : fileItems) {
            if (fileItem.isFormField()) {//如果是普通字段
                fieldName = fileItem.getFieldName();
                if (fieldName.equalsIgnoreCase("title")) {
                    news.setTitle(fileItem.getString("UTF-8"));
                } else if (fieldName.equalsIgnoreCase("id")) {
                    String id = fileItem.getString();
                    if (StringUtils.isNotBlank(id)) {
                        news.setId(Integer.parseInt(id));
                    }
                } else if (fieldName.equalsIgnoreCase("categoryId")) {
                    news.setCategoryId(Integer.parseInt(fileItem.getString()));
                } else if (fieldName.equalsIgnoreCase("summary")) {
                    news.setSummary(fileItem.getString("UTF-8"));
                } else if (fieldName.equalsIgnoreCase("newscontent")) {
                    news.setContent(fileItem.getString("UTF-8"));
                } else if (fieldName.equalsIgnoreCase("author")) {
                    news.setAuthor(fileItem.getString("UTF-8"));
                }
            } else {
                String fileName = fileItem.getName();
                if (StringUtils.isNotBlank(fileName)) {
                    File fullFile = new File(fileItem.getName());
                    File saveFile = new File(uploadFilePath, fullFile.getName());

                    fileItem.write(saveFile);
                    uploadFileName = fullFile.getName();
                    news.setPicPath(uploadFileName);

                    bUpload = true;
                }
            }
        }
    }
    System.out.println("上传成功后的文件名是：" + news.getPicPath());
    bRet = newsService.addNews(news);

    if (bRet) {
        response.sendRedirect("newsDetailList.jsp");
    } else {
        response.sendRedirect("newsDetailCreate.jsp");

    }

%>


</body>
</html>
